/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package org.swan.plugin.bean;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.type.TypeFactory;

import java.util.List;
import java.util.Map;

public class TypeBean {
    private String clazz;

    private TypeBean[] parameterTypes;
    private TypeBean array;

    private TypeBean collection;

    private TypeBean mapKey;
    private TypeBean mapValue;

    JavaType getType(TypeFactory typeFactory) throws ClassNotFoundException {
        if (clazz != null) {
            if (getParameterTypes() == null) {
                return typeFactory.constructType(Class.forName(clazz));
            } else {
                JavaType[] types = new JavaType[parameterTypes.length];
                for (int i = 0; i < types.length; i++) {
                    types[i] = parameterTypes[i].getType(typeFactory);
                }
                return typeFactory.constructParametricType(Class.forName(clazz), types);
            }
        } else if (array != null) {
            return typeFactory.constructArrayType(array.getType(typeFactory));
        } else if (collection != null) {
            return typeFactory.constructCollectionType(List.class, collection.getType(typeFactory));
        } else if (mapKey != null && mapValue != null) {
            return typeFactory.constructMapType(Map.class, mapKey.getType(typeFactory), mapValue.getType(typeFactory));
        }
        return null;
    }

    public TypeBean() {
        this(null, null);
    }

    public TypeBean(String clazz) {
        this(clazz, null);
    }

    public TypeBean(String clazz, TypeBean[] parameterTypes) {
        this.clazz = clazz;
        this.parameterTypes = parameterTypes;
    }

    public String getClazz() {
        return clazz;
    }

    public TypeBean setClazz(String clazz) {
        this.clazz = clazz;
        return this;
    }

    public TypeBean[] getParameterTypes() {
        return parameterTypes;
    }

    public TypeBean setParameterTypes(TypeBean[] parameterTypes) {
        this.parameterTypes = parameterTypes;
        return this;
    }

    public TypeBean getArray() {
        return array;
    }

    public TypeBean setArray(TypeBean array) {
        this.array = array;
        return this;
    }

    public TypeBean getCollection() {
        return collection;
    }

    public TypeBean setCollection(TypeBean collection) {
        this.collection = collection;
        return this;
    }

    public TypeBean getMapKey() {
        return mapKey;
    }

    public TypeBean setMapKey(TypeBean mapKey) {
        this.mapKey = mapKey;
        return this;
    }

    public TypeBean getMapValue() {
        return mapValue;
    }

    public TypeBean setMapValue(TypeBean mapValue) {
        this.mapValue = mapValue;
        return this;
    }
}
